//
// Created by 67678 on 2023/3/14.
//
#include <iostream>
#include <string>
#include <thread>

#include "../threadPool/threadPool.h"
#include "../../util/testInit.h"
#include "../../socket/socketClient.h"
#include "../../socket/socketServer.h"
#include "eventLoop.h"
#include "event.h"
#include "dispatcher.h"

using namespace std;
using namespace event;
using namespace socket_epoll;


void testEvent() {
    threadPool::ThreadPool::getInstance().init(4);
    Dispatcher &dp = Dispatcher::getInstance();
    dp.registerHandler(66, [](shared_ptr<Event> v) {
        cout << "testing" << endl;
        cout << v->type.num << endl;

    });
    EventLoop &eventLoop = EventLoop::getInstance();
    eventLoop.init();
    eventLoop.loopBackground();
}

void testSocketHandler() {
    EventLoop &eventLoop = EventLoop::getInstance();
    SocketServer &socketServer = SocketServer::getInstance();

    SocketClient socketClient;
    thread t([&]() {
        while (true)
            for (auto &event: socketServer.getEvents())
                eventLoop.addEvent(event);
    });
    t.detach();

    socketClient.connect(SocketServer::IP, SocketServer::PORT);

    socketClient.write("this is client!");
    this_thread::sleep_for(5s);

    //string str = socketClient.read();
    //cout << str<<endl;

    socketClient.close();
    this_thread::sleep_for(100s);
}

int main() {
    init();
    testSocketHandler();
}